package ru.stepuk.modern.distributions;

import ru.stepuk.modern.generators.RandomNumberGenerator;

/**
 *
 * @author ivan
 */
public class DiscreteDistribution implements Distribution {

    private int[] p;
    private double[] x;
    private RandomNumberGenerator g;

    public DiscreteDistribution(RandomNumberGenerator g, int[] p, double[] x) {
        this.p = p;
        this.x = x;
        this.g = g;
    }

    public RandomNumberGenerator getGenerator() {
        return g;
    }

    public double nextValue() {
        double rnd = g.nextValue() * 100;
        double sum = 0;

        for (int i = 0; i < x.length - 1; i++) {
            sum += p[i];
            if (rnd < sum) {
                return x[i];
            }
        }
        return x[x.length - 1];
    }

    public int[] getP() {
        return p;
    }

    public double[] getX() {
        return x;
    }



    @Override
    public String toString() {
        return "discrete";
    }

}
